<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="月卡续费" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carInfo" :model="carInfoForm" :rules="carInfoRules" label-width="100px">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="carInfoForm.personName" disabled="" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="carInfoForm.phoneNumber" disabled="" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="carInfoForm.carNumber" disabled="" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="carInfoForm.carBrand" disabled="" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="feeForm" :model="feeForm" :rules="feeFormRules" label-width="100px">
            <el-form-item label="有效日期" prop="payTime">
              <!--
                value-format="yyyy-MM-dd" 表示日期格式为年-月-日，如果不绑定，那么默认为时间对象
                start-placeholder="开始日期" 表示开始日期的占位符
                end-placeholder="结束日期" 表示结束日期的占位符
                v-model="feeForm.payTime" 表示绑定日期数据
                type="daterange" 表示日期范围选择器
                  当type 为date 时，就一个值，为daterange时，就两个值，分别为开始日期和结束日期
                range-separator="至" 表示日期范围的分割符
               -->
              <el-date-picker
                v-model="feeForm.payTime"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod" clearable>
                <el-option
                  v-for="item in payMethodList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="renewMonthCard">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { renewMonthCardAPI, addRenewMonthCardAPI } from '@/apis/car'
export default {
  data() {
    return {
      // 车辆信息表单
      carInfoForm: {
        personName: '', // 车主姓名
        phoneNumber: '', // 联系方式
        carNumber: '', // 车牌号码
        carBrand: '' // 车辆品牌
      },
      // 车辆信息表单验证规则
      carInfoRules: {
        personName: [
          {
            required: true, message: '请输入车主姓名', trigger: 'blur'
          },
          {
            validator: this.validatorPersonName, trigger: 'blur'
          }
        ],
        phoneNumber: [
          {
            required: true, message: '请输入联系方式', trigger: 'blur'
          },
          {
            validator: this.validatorPhoneNumber, trigger: 'blur'
          }
        ],
        carNumber: [
          {
            required: true, message: '请输入车辆号码', trigger: 'blur'
          },
          {
            validator: this.validatorCarNumber, trigger: 'blur'
          }
        ],
        carBrand: [
          {
            required: true, message: '请输入车辆品牌', trigger: 'blur'
          },
          {
            validator: this.validatorCarName, trigger: 'blur'
          }
        ]
      },
      // 缴费信息表单
      feeForm: {
        payTime: [], // 支付时间
        paymentAmount: null, // 支付金额
        paymentMethod: null // 支付方式
      },
      // 缴费信息验证规则
      feeFormRules: {
        payTime: [
          {
            required: true,
            message: '请选择支付时间'
          }
        ],
        paymentAmount: [
          {
            required: true,
            message: '请输入支付金额',
            trigger: 'blur'
          },
          {
            validator: this.validatorpaymentAmount, trigger: 'blur'
          }
        ],
        paymentMethod: [
          {
            required: true,
            message: '请选择支付方式',
            trigger: 'change'
          }
        ]
      },
      // 支付方式列表
      payMethodList: [
        {
          id: 'Alipay',
          name: '支付宝'
        },
        {
          id: 'WeChat',
          name: '微信'
        },
        {
          id: 'Cash',
          name: '线下'
        }
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      }
    }
  },
  mounted() {
    this.getCarList()
  },
  methods: {
    validatorpaymentAmount(rule, value, callback) {
      if (/^\d+$/.test(value)) callback()
      else callback(new Error('请输入正确的金额'))
    },
    // 获取月卡信息
    async getCarList() {
      const res = await renewMonthCardAPI(this.$route.query.id)
      const { personName, phoneNumber, carNumber, carBrand, paymentAmount, paymentMethod } = res.data
      this.carInfoForm = { personName, phoneNumber, carNumber, carBrand }
      this.feeForm.paymentAmount = paymentAmount
      this.feeForm.paymentMethod = paymentMethod
      this.feeForm.payTime = [res.data.cardStartDate, res.data.cardEndDate]
    },
    // 续费提交
    async renewMonthCard() {
      const obj = {
        carInfoId: this.$route.query.id,
        cardStartDate: this.feeForm.payTime[0],
        cardEndDate: this.feeForm.payTime[1],
        paymentAmount: this.feeForm.paymentAmount,
        paymentMethod: this.feeForm.paymentMethod
      }
      await addRenewMonthCardAPI(obj)
      this.$router.back()
      this.$message.success('续费成功')
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
